

package com.ce.cases.assit.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ce.cases.assit.dao.po.CaseInvestigation;
import com.ce.cases.source.dao.po.CaseSource;
import com.ce.cases.source.dao.po.CaseSourceDrawApprove;
import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.datamaintain.dao.po.AbstractPO;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.framework.util.GeneratorUUID;
import com.ce.framework.util.StringUtil;
import com.ce.sys.login.model.User;
import com.ce.sys.person.dao.po.SysPerson;
import com.ce.util.OrgUtil;

public class CaseInvestigationAction extends DataMaintainAction 
{
	@Override
	protected String addItem() throws AppException {
		String inspectionDeptId=this.getParameter("po.inspectionDeptId");
		String collectionDeptId=this.getParameter("po.collectionDeptId");
		String jcjName=OrgUtil.getOrgName(inspectionDeptId);
		String zgjName=OrgUtil.getOrgName(collectionDeptId);
		try {
			BeanAccessUtil.copyProperty(po, "registerDate", new Date());
			BeanAccessUtil.copyProperty(po, "registerPerson",User.getUserName());
			BeanAccessUtil.copyProperty(po, "registerDeptId",User.getUserOrgId());
			BeanAccessUtil.copyProperty(po, "registerDept", User.getUserOrgName());
			BeanAccessUtil.copyProperty(po, "state", "0");
			BeanAccessUtil.copyProperty(po, "stateInfo", "");
			BeanAccessUtil.copyProperty(po, "inspectionDept", jcjName);
			BeanAccessUtil.copyProperty(po, "collectionDept", zgjName);
			BeanAccessUtil.copyProperty(po, "isCc", "0");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return super.addItem();
	}

	@Override
	protected String modifyItem() throws AppException {
		CaseInvestigation entiy=(CaseInvestigation)this.baselogic.getPOByPk(po.getClass().getName(), po.getVcId());
		BeanAccessUtil.copyNoCopyNullProperty(po, entiy);
		CaseInvestigation po2=(CaseInvestigation)po;
		String jcjName=OrgUtil.getOrgName(po2.getInspectionDeptId());
		String zgjName=OrgUtil.getOrgName(po2.getCollectionDeptId());
		entiy.setInspectionDept(jcjName);
		entiy.setCollectionDept(zgjName);
		this.baselogic.updatePo(entiy,this.getAddList(),this.getModifyList(),this.getDeleteList());
	    return super.searchData();			
	}
	
	/**
	 * 打开回复操作页
	 */
	public String replyPage() throws AppException {
		String vcId = this.getSelectId();
	    try {
	    	po=(AbstractPO)this.baselogic.getPOByPk(po.getClass().getName(), vcId);
	    	CaseInvestigation check=(CaseInvestigation)po;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openReplyPage";
	}

	/**
	 * 回复保存
	 */
	public String dealReply() {
		try {
			CaseInvestigation savePo = (CaseInvestigation) this.baselogic.getPOByPk(po.getClass().getName(), po.getVcId());
			//注：修改时无法拷贝置空(非必填项)的数据，适合数据添加
			BeanAccessUtil.copyNoCopyNullProperty(po, savePo);
			savePo.setReplyDate(new Date());
			savePo.setReplyPersonId(User.getUserId());
			savePo.setReplyPersonName(User.getUserOrgName());
			this.baselogic.updatePo(savePo);
		} catch (AppException e) {
			e.printStackTrace();
			return this.returnInfoForJS(false, e.getMessage());
		}
		return this.returnInfoForJS(true, "回复情况保存成功！");
	}
	
	/**
	 * 打开转立案操作页
	 */
	public String laPage() throws AppException {
		String vcId = this.getSelectId();
	    try {
	    	po=(AbstractPO)this.baselogic.getPOByPk(po.getClass().getName(), vcId);
	    	CaseInvestigation check=(CaseInvestigation)po;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openLaPage";
	}

	/**
	 * 保存立案操作
	 */
	public String saveLa() {
		try {
			CaseInvestigation savePo = (CaseInvestigation) this.baselogic.getPOByPk(po.getClass().getName(), po.getVcId());
			//注：修改时无法拷贝置空(非必填项)的数据，适合数据添加
			BeanAccessUtil.copyNoCopyNullProperty(po, savePo);
			savePo.setState(1);//协查专立案
			savePo.setCaseFromNum("9");//默认			
			savePo.setCaseFromName("协查");
			savePo.setHandleDate(new Date());
			savePo.setHandlePerson(User.getUserName());	
			savePo.setIsCc(1);			
			this.baselogic.updatePo(savePo);
		} catch (AppException e) {
			e.printStackTrace();
			return this.returnInfoForJS(false, e.getMessage());
		}
		return this.returnInfoForJS(true, "转立案成功！");
	}
	
	
	/**
	 * 受理前期判断该企业是否正在被查。
	 * @return
	 */
	public String saveNewAcceptSuccessedBefore(){
		String[] vcIds=this.getParameterValues("chkSelf[]");
		String acceptType=this.getParameter("acceptType");//是否受理
		String sql_ids=StringUtil.fromArrayToStr(vcIds, true);
		List<CaseInvestigation> l=new ArrayList<CaseInvestigation>();
		try {
			l = (List<CaseInvestigation>)this.baselogic.searchByHql("FROM CaseInvestigation p where p.vcDelFlag='0' and p.vcId in("+sql_ids+")");
		} catch (AppException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		CaseInvestigation po=new CaseInvestigation();
		if(l!=null&&l.size()>0){
			if("1".equals(acceptType))//受理
			{
				for(CaseInvestigation p:l){
					try {
						List<CaseSource> pos=(List<CaseSource>)this.baselogic.searchByHql("FROM CaseSource p where p.vcDelFlag='0' and p.taxpayerNumber='"+p.getTaxpayerNumber()+"'");
						if(pos!=null&&pos.size()>0){
							CaseSource temp=pos.get(0);
							if(3==temp.getState())
								return this.returnInfoForJS(false,"纳税人识别号为"+p.getTaxpayerNumber()+",涉案企业"+ p.getInvolvedEnterprice()+"已在已选库，确定受理吗？");
							else
								return this.returnInfoForJS(false,"纳税人识别号为"+p.getTaxpayerNumber()+",涉案企业"+ p.getInvolvedEnterprice()+"已在备选库，确定受理吗？");
						}
					} catch (AppException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		}
		
		return this.returnInfoForJS(true,"");
	}
	
	
	/**
	 * 重新受理
	 * @return
	 */
	public String saveNewAcceptSuccessed(){
		//String vcId=this.getParameter("chkSelf");
		String[] vcIds=this.getParameterValues("chkSelf[]");
		String acceptType=this.getParameter("acceptType");//是否受理
		CaseInvestigation po=new CaseInvestigation();
		
		String sql_ids=StringUtil.fromArrayToStr(vcIds, true);
		List<CaseInvestigation> l=new ArrayList<CaseInvestigation>();
		try {
			l = (List<CaseInvestigation>)this.baselogic.searchByHql("FROM CaseInvestigation p where p.vcDelFlag='0' and p.vcId in("+sql_ids+")");
		} catch (AppException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(l!=null&&l.size()>0){
			for(CaseInvestigation p:l){	
				
				p.setOperatePersonName(User.getUserName());
				p.setOperatePerson(User.getUserId());
				p.setOperateDate(new Date());				
				
				if("1".equals(acceptType)){//受理involvedEnterprice					
					boolean b=true;
					p.setState(2);//协查专立案成功
					if(StringUtil.isNotBlank(p.getCaseId())){
						CaseSource case_po=new CaseSource();
						try {
							case_po=(CaseSource)this.baselogic.getPOByPk(case_po.getClass().getName(), p.getCaseId());
						} catch (AppException e) {
							e.printStackTrace();
						}
						if(case_po!=null){
							b=false;
							case_po.setVcAaccept("1");//受理		
							case_po.setVcDelFlag(0);
							try {
								this.baselogic.updatePo(case_po);
							} catch (AppException e) {
								e.printStackTrace();
							}
						}								
						
					}					
					if(b){
						CaseSource case_po=new CaseSource();
						case_po.setVcId(GeneratorUUID.generateUUID());
						case_po.setVcDelFlag(0);
						case_po.setVcType("3");//协查转立案
						case_po.setState(0);
						case_po.setVcAaccept("1");//已受理
						case_po.setRegisterDept(p.getRegisterDept());
						case_po.setRegisterDeptId(p.getRegisterDeptId());
						case_po.setRegisterPerson(p.getRegisterPerson());
						case_po.setReceiveDate(new Date());
						case_po.setRegisterDate(new Date());
						case_po.setInspectionDeptId(p.getInspectionDeptId());
						case_po.setInspectionDept(p.getInspectionDept());
						case_po.setCollectionDeptId(p.getCollectionDeptId());
						case_po.setCollectionDept(p.getCollectionDept());
						case_po.setTaxpayerNumber(p.getTaxpayerNumber());
						case_po.setInvolvedEnterprice(p.getInvolvedEnterprice());
						case_po.setCaseFromNum(p.getCaseFromNum());
						case_po.setCaseFromName(p.getCaseFromName());						
						case_po.setIsImportant(p.getIsImportant());
						case_po.setIsSecret(p.getIsSecret());				
						case_po.setBaseResult(p.getDrawReason());					
						p.setCaseId(case_po.getVcId());						
						try {
							this.baselogic.addPo(case_po);
						} catch (AppException e) {
							e.printStackTrace();
						}		
					}
					
					
				}else if("2".equals(acceptType)){
					p.setState(-2);//协查专立案不成功
					if(StringUtil.isNotBlank(p.getCaseId())){
						CaseSource case_po=new CaseSource();
						try {
							case_po=(CaseSource)this.baselogic.getPOByPk(case_po.getClass().getName(), p.getCaseId());
						} catch (AppException e) {
							e.printStackTrace();
						}
						case_po.setVcAaccept("2");//不予受理					
						try {
							this.baselogic.updatePo(case_po);
						} catch (AppException e) {
							e.printStackTrace();
						}		
						
					}
				}
				try {
					this.baselogic.updatePo(p);
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
			}
		}		
		return this.returnInfoForJS(true, "操作成功！");
	}
	
	
	
}